package net.maku.events.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import lombok.AllArgsConstructor;
import net.maku.events.entity.ActivityContestQuestionOptionEntity;
import net.maku.events.service.ActivityContestQuestionOptionService;
import net.maku.events.vo.ActivityContestQuestionOptionVO;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.events.entity.ActivityContestQuestionEntity;
import net.maku.events.vo.ActivityContestQuestionVO;
import net.maku.events.dao.ActivityContestQuestionDao;
import net.maku.events.service.ActivityContestQuestionService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 竞赛答题
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-22
 */
@Service
@AllArgsConstructor
public class ActivityContestQuestionServiceImpl extends BaseServiceImpl<ActivityContestQuestionDao, ActivityContestQuestionEntity> implements ActivityContestQuestionService {

    private final ActivityContestQuestionOptionService activityContestQuestionOptionService;

    @Override
    public void save(Long businessId, Integer businessType, List<ActivityContestQuestionVO> questionVOList) {
        if (CollectionUtils.isEmpty(questionVOList)) {
            return;
        }
        questionVOList.forEach(item -> {
            ActivityContestQuestionEntity activityContestQuestionEntity = BeanUtil.copyProperties(item, ActivityContestQuestionEntity.class);
            activityContestQuestionEntity.setBusinessId(businessId);
            activityContestQuestionEntity.setBusinessType(businessType);
            this.save(activityContestQuestionEntity);
            activityContestQuestionOptionService.save(activityContestQuestionEntity.getId(), item.getActivityContestQuestionOptionList());
        });

    }

    @Override
    public void delete(Integer businessType, List<Long> businessIdList) {
        List<ActivityContestQuestionEntity> questionEntityList = this.list(new LambdaQueryWrapper<ActivityContestQuestionEntity>().eq(ActivityContestQuestionEntity::getBusinessType, businessType)
                .in(ActivityContestQuestionEntity::getBusinessId, businessIdList));
        if (CollectionUtils.isNotEmpty(questionEntityList)) {
            List<Long> questionIdList = questionEntityList.stream().map(ActivityContestQuestionEntity::getId).collect(Collectors.toList());
            activityContestQuestionOptionService.remove(new LambdaQueryWrapper<ActivityContestQuestionOptionEntity>().in(ActivityContestQuestionOptionEntity::getContestQuestionId, questionIdList));
            this.removeByIds(questionIdList);
        }

    }

    @Override
    public List<ActivityContestQuestionVO> findByBusinessIdList(List<Long> businessIdList, Integer businessType) {
        List<ActivityContestQuestionEntity> questionEntityList = this.list(new LambdaQueryWrapper<ActivityContestQuestionEntity>().eq(ActivityContestQuestionEntity::getBusinessType, businessType)
                .in(ActivityContestQuestionEntity::getBusinessId, businessIdList));
        return shareSetOption(questionEntityList);
    }

    @Override
    public List<ActivityContestQuestionVO> findByQuestionIdList(List<Long> questionIdList) {
        List<ActivityContestQuestionEntity> questionEntityList = this.listByIds(questionIdList);
        return shareSetOption(questionEntityList);
    }

    /**
     * 设置选项数据
     *
     * @param questionEntityList 题目数据
     * @return 结果
     */
    private List<ActivityContestQuestionVO> shareSetOption(List<ActivityContestQuestionEntity> questionEntityList) {
        if (CollectionUtils.isNotEmpty(questionEntityList)) {
            List<ActivityContestQuestionOptionVO> questionOptionVOList = BeanUtil.copyToList(activityContestQuestionOptionService.list(new LambdaQueryWrapper<ActivityContestQuestionOptionEntity>().in(ActivityContestQuestionOptionEntity::getContestQuestionId, questionEntityList.stream().map(ActivityContestQuestionEntity::getId).collect(Collectors.toList()))), ActivityContestQuestionOptionVO.class);
            Map<Long, List<ActivityContestQuestionOptionVO>> optionMap = questionOptionVOList.stream().collect(Collectors.groupingBy(ActivityContestQuestionOptionVO::getContestQuestionId));
            List<ActivityContestQuestionVO> questionVOList = BeanUtil.copyToList(questionEntityList, ActivityContestQuestionVO.class);
            questionVOList.forEach(item -> {
                item.setActivityContestQuestionOptionList(optionMap.get(item.getId()));
            });
            return questionVOList;
        }
        return List.of();
    }
}